package unilearn.generate;

import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;

/*
 * GenerateSubject.java
 *
 * Created on 29 April 2008, 22:39
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

/**
 * @author Ben
 */
public class GenerateSubject
{

	/** Number of Subjects to Generate */
	public static final int GENERATE_SUBJECT_ID = 10;

	public static final String INSERT_SUBJECT = "INSERT INTO subject"
			+ " (subject_code, subject_name, semester, year_running, description, convenor_name, convenor_email) "
			+ "VALUES ('%1','TEST_SUBJECT_%2',1,2008,'This is test subject #%2','Example Conveynor','example@example.com');";

	private GenerateAssignments assignmentsGen;

	private PrintWriter writer;

	/** Creates a new instance of GenerateSubject */
	public GenerateSubject(PrintWriter writer)
	{
		this(null, writer);
	}

	/** Creates a new instance of GenerateSubject */
	public GenerateSubject(GenerateAssignments assignmentsGen, PrintWriter writer)
	{
		this.assignmentsGen = assignmentsGen;
		this.writer = writer;
	}

	private long subjectsCreated = 0;

	public void addSubjects(Set<String> studentCodes) throws SQLException
	{
		Set<String> subjectCodes = new HashSet<String>();
		do
		{
			subjectCodes.add(generateSubjectCode());
		} while (subjectCodes.size() != GENERATE_SUBJECT_ID);

		for (String s : subjectCodes)
		{
			subjectsCreated++;
			String value = INSERT_SUBJECT;
			value = value.replace("%1", s);
			value = value.replace("%2", String.valueOf(subjectsCreated));
			writer.println(value);

			if (assignmentsGen != null)
			{
				String[] subjectStudents = getSubjectStudents(studentCodes);
				assignmentsGen.addAssignments(subjectsCreated, subjectStudents);
			}
		}
	}

	private String[] getSubjectStudents(Set<String> studentCodes)
	{
		Set<String> subjectStudents = new HashSet<String>();
		int students = (int) (Math.random() * 7.0);
		int var;
		List<String> candidates = new LinkedList<String>();
		candidates.addAll(studentCodes);

		students += 20;
		do
		{
			var = (int) (Math.random() * candidates.size());
			subjectStudents.add(candidates.remove(var));
		} while (subjectStudents.size() != students);

		return subjectStudents.toArray(new String[0]);
	}

	private String generateSubjectCode()
	{
		int val;
		int digits = 3;

		StringBuilder builder = new StringBuilder();
		val = (int) (Math.random() * 3.0);
		switch (val)
		{
			case (0) :
				builder.append("HIT");
				digits = 4;
				break;
			case (1) :
				builder.append("HMS");
				break;
			default :
				builder.append("HET");
				break;
		}
		builder.append("-");
		val = (int) (Math.random() * 10.0);
		builder.append(val + 1);
		for (int i = 0; i < digits - 1; i++)
		{
			val = (int) (Math.random() * 10.0);
			builder.append(val);
		}

		return builder.toString();
	}

}
